//
//  FXToolTipController.m
//  Cocoa-Fetion
//
//  Created by Ronnie on 08-12-27.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "FXToolTipController.h"

#define TOOL_TIP_CHECK_INTERVAL			45.0	//Check for mouse X times a second
#define DEFAULT_TOOL_TIP_DELAY			50.0	//Number of check intervals of no movement before a tip is displayed
#define TOOL_TIP_TO_MOUSE_DIS			10.0

@implementation FXToolTipController

- (id)initWithView:(NSView*)aView
{
	self = [super init];
	if (![super initWithWindowNibName:@"TooltipWindow"])
		return nil;
	view = [aView retain];
	
	m_tooltipDelay = DEFAULT_TOOL_TIP_DELAY;
	tooltipMouseLocationTimer = nil;
	dataNode = nil;
	
	oldMouseLocation = NSZeroPoint;
	oldToolTipLocation = NSZeroPoint;
	
//	NSRect rect = [[[view superview] superview] visibleRect];
//	//NSLog(@"track rect is (%f, %f, %f, %f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
//	
//	BOOL mouseIsIn = NSPointInRect([view convertPoint:[[view window] convertScreenToBase:[NSEvent mouseLocation]] fromView:[[view window] contentView]], rect);
//	tooltipTrackingTag = [[[view superview] superview] addTrackingRect:rect owner:self userData:nil assumeInside:NO];
	[self storeTrackingArea];
	return self;
}

- (void) dealloc
{
	[dataNode release];
	[view release];
	[super dealloc];
}

- (void)storeTrackingArea
{
	if ( tooltipTrackingTag!=-1 ) {
		[view removeTrackingRect:tooltipTrackingTag];
		tooltipTrackingTag = -1;
	}
	NSRect rect = [[[view superview] superview] visibleRect];
	
	NSLog(@"track rect is (%f, %f, %f, %f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
	BOOL mouseIsIn = NSPointInRect([view convertPoint:[[view window] convertScreenToBase:[NSEvent mouseLocation]] fromView:[[view window] contentView]], rect);
	NSPoint convertedPoint = [view convertPoint:[[view window] convertScreenToBase:[NSEvent mouseLocation]] fromView:[[view window] contentView]];
	NSLog(@"convertedPoint is (%f, %f),  %d", convertedPoint.x, convertedPoint.y, mouseIsIn);
	tooltipTrackingTag = [[[view superview] superview] addTrackingRect:rect owner:self userData:nil assumeInside:NO];
	if (mouseIsIn) {
		[self _stopTrackingMouse];
		[self _startTrackingMouse];
	}
}

- (void)mouseEntered:(NSEvent *)theEvent {
	NSLog(@"Mouse Entered");
	[self _startTrackingMouse];
}

- (void)mouseExited:(NSEvent *)theEvent {
	NSLog(@"Mouse Exited");
	[self _stopTrackingMouse];
	[[self window] orderOut:self];
}

- (void)_startTrackingMouse {
	if (!tooltipMouseLocationTimer) {
		tooltipCount = 0;
		tooltipMouseLocationTimer = [[NSTimer scheduledTimerWithTimeInterval:(1.0 / TOOL_TIP_CHECK_INTERVAL)
																	  target:self
																	selector:@selector(mouseMovementTimer:)
																	userInfo:nil
																	 repeats:YES] retain];
	}
}

- (void)_stopTrackingMouse {
	//Invalidate tracking
	if (tooltipMouseLocationTimer) {
		
		NSTimer* theTimer = tooltipMouseLocationTimer;
		tooltipMouseLocationTimer = nil;
		
		[theTimer invalidate];
		[theTimer release]; 
		theTimer = nil;
	}
}

- (void)hideTooltip {
	[self _stopTrackingMouse];
	oldMouseLocation = NSZeroPoint;
	oldToolTipLocation = NSZeroPoint;
	[[self window] orderOut:self];
}


- (void)setDataNode:(FXTreeNode*)node
{
	if (node != dataNode) {
		[node release];
		dataNode = node;
		[dataNode retain];
		
		const FXUserInfo* accountInfo = [dataNode getUserInfo];
		
		
		const char* mobileNumP = [accountInfo getFetionAccount]->personal?[accountInfo getFetionAccount]->personal->mobile_no:"";
		NSString* userID = nil;
		if (![accountInfo userID]) {
			userID = [NSString stringWithUTF8String:mobileNumP?mobileNumP:""];
		}
		else
			userID = [NSString stringWithFormat:@"%ld", [accountInfo userID]];
		
		[numberField setStringValue:userID];
				
		const char* nickNameP = [accountInfo getFetionAccount]->personal?[accountInfo getFetionAccount]->personal->nickname:"";
		NSString* nickName = [NSString stringWithUTF8String:nickNameP?nickNameP:""];
		[nickNameField setStringValue:nickName];
		
		[impresaField setStringValue:[accountInfo userImpresa]];
		
		[[self window] setTitle:[accountInfo userName]];
	}
}

- (void)mouseMovementTimer:(NSTimer *)inTimer {
	NSPoint		mouseLocation = [NSEvent mouseLocation];
	NSWindow	*theWindow = [view window];
	NSPoint		viewPoint = [theWindow convertScreenToBase:mouseLocation];
	NSRect		viewFrame = [view frame];
	NSRect		viewRect = [[theWindow contentView] convertRect:[view frame] fromView:[view superview]];
	//NSLog(@"viewFrame is (%f, %f, %f, %f)", viewFrame.origin.x, viewFrame.origin.y, viewFrame.size.width, viewFrame.size.height);
	//NSLog(@"viewRect  is (%f, %f, %f, %f)", viewRect.origin.x, viewRect.origin.y, viewRect.size.width, viewRect.size.height);
				
	if ([theWindow isVisible] && NSPointInRect([theWindow convertScreenToBase:mouseLocation],[[theWindow contentView] convertRect:[view frame] fromView:[view superview]])) 
	{
		if (tooltipCount > m_tooltipDelay) {
			if (!NSEqualPoints(mouseLocation, oldToolTipLocation))
			{
				NSPoint winLocation = [theWindow convertScreenToBase:mouseLocation];
				NSPoint viewLocation = [view convertPoint:winLocation fromView:nil];
				//NSLog(@"Window location (%f, %f)", winLocation.x, winLocation.y);
				//NSLog(@"View location   (%f, %f)", viewLocation.x, viewLocation.y);
				
				FXTreeNode* node = nil;
				int row = [view rowAtPoint:viewLocation];
				
				if (row!=-1) {
					node = [view itemAtRow:row];
					if ([node isLeaf]) {
						//NSLog(@"Show Tips of %@", [[node getUserInfo] userName]);
						NSRect tipWindowFrame = [[self window] frame];
						tipWindowFrame.origin = mouseLocation;
						tipWindowFrame.origin.y -= (tipWindowFrame.size.height + TOOL_TIP_TO_MOUSE_DIS);
						tipWindowFrame.origin.x += TOOL_TIP_TO_MOUSE_DIS;
						
						[self setDataNode:node];
						[[self window] setFrame:tipWindowFrame display:YES];
						
						[[self window] orderFront:self];
					}
					else
					{
						[[self window] orderOut:self];
					}
				}
				oldToolTipLocation = mouseLocation;
				//tooltipCount++;
			}
		} 
		else 
		{
			if (!NSEqualPoints(mouseLocation, oldMouseLocation)) {
				tooltipCount = 0;
				oldMouseLocation = mouseLocation;
			}
			else
			{
				tooltipCount++;
			}
		}
		
		//NSLog(@"%d", tooltipCount);
	} 
	else 
	{
		[[self window] orderOut:self];
	}
}



@end
